package com.tobey.logfetch.controller;

import com.tobey.logfetch.params.RequestParam;
import com.tobey.logfetch.util.RemoteExecuteCommand;
import com.tobey.logfetch.vo.ResultVo;
import io.swagger.annotations.*;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author tangbing
 * @date 2019/12/31 17:18
 */
@Api(value = "test", description = "测试")
@RestController
@RequestMapping(value = "/test")
public class TestController {

    /**
     * @param ip
     * @return
     */
    @ApiOperation(value = "连接ssh", notes = "连接ssh")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ip", value = "车道IP", required = true, dataType = "String", paramType = "Path"),
            @ApiImplicitParam(name = "netNo", value = "收费站号前两位", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "plazaNo", value = "收费站号后两位", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "laneNo", value = "车道号", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "laneType", value = "入口车道填7，出口车道填1", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "laneAttribute", value = "封闭式填1，开放式填2", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "curTollGrantryNo", value = "门架编号", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "gantryHex", value = "门架Hex编码", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "newServerIp", value = "新服务器地址", required = true, dataType = "String", paramType = "form")
    })
    @PostMapping("connect/{ip}")
    public ResultVo connect(@ApiIgnore @PathVariable("ip") String ip, RequestParam requestParam) {
        return RemoteExecuteCommand.sshCommand(ip, requestParam);
    }
    /**
     * @param ip
     * @return
     */
    @ApiOperation(value = "连接ssh2", notes = "连接ssh2")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ip", value = "车道IP", required = true, dataType = "String", paramType = "Path"),
            @ApiImplicitParam(name = "netNo", value = "收费站号前两位", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "plazaNo", value = "收费站号后两位", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "laneNo", value = "车道号", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "laneType", value = "入口车道填7，出口车道填1", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "laneAttribute", value = "封闭式填1，开放式填2", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "curTollGrantryNo", value = "门架编号", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "gantryHex", value = "门架Hex编码", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "newServerIp", value = "收费站信服务器地址", required = true, dataType = "String", paramType = "form")
    })
    @PostMapping("connect2/{ip}")
    public String connect2(@ApiIgnore @PathVariable("ip") String ip, RequestParam requestParam) {
        StringBuilder sb = new StringBuilder();
        ResultVo resultVo = RemoteExecuteCommand.sshCommand(ip, requestParam);

        List<String> config = resultVo.getConfig();
        if (config == null) {
            return "链接服务器超时";
        }
        if (!CollectionUtils.isEmpty(config)) {
            sb.append("Config.ini配置文件\n");
            for (String s : config) {
                sb.append(s);
                sb.append("\n");
            }
        }
        sb.append("\n");
        List<String> appFunction = resultVo.getAppFunction();
        if (!CollectionUtils.isEmpty(appFunction)) {
            sb.append("AppFunction.ini配置文件\n");
            for (String s : appFunction) {
                sb.append(s);
                sb.append("\n");
            }
        }
        sb.append("\n");
        List<String> communctionConfig = resultVo.getCommunctionConfig();
        if (!CollectionUtils.isEmpty(communctionConfig)) {
            sb.append("CommunctionConfig.ini配置文件\n");
            for (String s : communctionConfig) {
                sb.append(s);
                sb.append("\n");
            }
        }
        sb.append("\n");
        Boolean faremtcDb = resultVo.getFaremtcDb();
        if(!faremtcDb) {
            sb.append("FAREMTC.db不存在");
            sb.append("\n");
        }
        sb.append("\n");
        Boolean fareetcDb = resultVo.getFareetcDb();
        if(!fareetcDb) {
            sb.append("FAREETC.db不存在");
            sb.append("\n");
        }

        Boolean farecpcDb = resultVo.getFarecpcDb();
        if(!farecpcDb) {
            sb.append("FARECPC.db不存在");
            sb.append("\n");
        }

        return sb.toString();
    }
}
